package com.hxht.cmp.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hxht.cmp.common.enums.ResultCode;
import com.hxht.cmp.common.result.ResultResponse;
import com.hxht.cmp.service.CasePlanService;
import com.hxht.cmp.websocket.VodWebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/playInfo")
public class PlayInfoConteroller {
    private Logger logger = LoggerFactory.getLogger(PlayInfoConteroller.class);
    @Autowired
    private CasePlanService casePlanService;
    //点播任务列表缓存
    private static String vodString = "";

    /**
     * 获取cmp正在点播的信息
     *
     * @return
     */
    @RequestMapping("/getPlayList")
    public ResultResponse getPlayList() {
        List<Map<String, String>> cmpPlayList = VodWebSocket.getCmpPlayList();
        if (ObjectUtils.isEmpty(cmpPlayList)) {
            return ResultResponse.init(1, "无点播信息", null);
        }
        return ResultResponse.success(cmpPlayList);
    }

    /**
     * 获取案件信息
     *
     * @param planUid
     * @return
     */
    @RequestMapping("getCaseInfoByUid")
    public ResultResponse getCaseInfoByUid(String planUid, String trialUid) {
        if (ObjectUtils.isEmpty(planUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(casePlanService.getCaseInfoByUid(planUid, trialUid));
    }

    /**
     * 处理system-monitor发送的dts点播信息
     *
     * @param json
     */
    @RequestMapping(value = "dtsPlanInfo", produces = "application/json;charset=UTF-8")
    public void planInfo(@RequestBody String json) {
        logger.info("planInfo方法入参:" + json);
        //保存
        vodString = json;
    }

    /**
     * gaoziwei 2017/3/15
     * 获取点播列表任务列表接口
     */
    @RequestMapping(value = "getUserList")
    public ResultResponse getUserList() {
        if (vodString.length() > 0) {
            //保存用户点播记录
            Map<String, String> obj = new Gson().fromJson(vodString, new TypeToken<Map<String, String>>() {
            }.getType());
            List<JsonObject> list = new ArrayList<>();
            obj.forEach((k, v) -> {
                JsonObject value = new Gson().fromJson(v, new TypeToken<JsonObject>() {
                }.getType());
                list.add(value);
            });
            return ResultResponse.success(list);

        }
        return ResultResponse.init(1, "无点播信息", null);
    }
}
